package aftersale

import (
	"context"

	"mall/common/globalkey"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/order/rpc/orderclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAfterSaleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 处理售后申请
func NewUpdateAfterSaleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAfterSaleLogic {
	return &UpdateAfterSaleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateAfterSaleLogic) UpdateAfterSale(req *types.AfterSaleInfo) (resp *types.BaseMsgResp, err error) {
	if req.Id == nil || *req.Id == 0 || req.AfterSaleType == nil || *req.AfterSaleType == 0 {
		return nil, resultx.NewErrCode(resultx.AFTER_SALE_PARAM_INVALID)
	}
	var result = &orderclient.AfterSaleInfo{}
	//确定售后类型   修改的售后状态
	var afterSaleType uint32
	if *req.AfterSaleType == globalkey.AfterSaleTypeRefund { // 退款
		afterSaleType = globalkey.AfterSaleTypeRefund
	} else if *req.AfterSaleType == globalkey.AfterSaleTypeReturn { // 退货
		afterSaleType = globalkey.AfterSaleTypeReturn
	} else if *req.AfterSaleType == globalkey.AfterSaleTypeBarter { // 换货
		afterSaleType = globalkey.AfterSaleTypeBarter
	}
	if req.ExpressSn != nil {
		result.ExpressSn = req.ExpressSn
	}
	result.Id = req.Id
	result.AfterSaleType = pointy.GetPointer(afterSaleType)
	_, err = l.svcCtx.OrderRpc.UpdateAfterSale(l.ctx, result)
	if err != nil {
		return nil, err
	}
	return &types.BaseMsgResp{
		Msg: "修改售后信息成功",
	}, nil
}
